package examples.spawners;

import examples.newEntityCreation.ExampleTheadBased2DEntity;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import model.Coordinate;
import model.entity.Entity2D;
import model.spawner.Spawner2D;

public class SimpleSpawnerExample extends Spawner2D implements KeyListener, MouseListener {

	//Variabili di supporto.
	
	private int minX;
	private int spawnRange;
	private int spawnY;
	private Random r;
	
	
	public SimpleSpawnerExample(int minX, int maxX, int spawnY) {
		this.minX = minX;
		this.spawnRange = maxX-minX;
		this.spawnY = spawnY;
		this.r = new Random();
	}

	@Override
	public void createEntity() {
		int ID = getBufferIndex();
		Coordinate coo = new Coordinate( minX + r.nextInt(spawnRange), spawnY, 0);
		Entity2D entity = new ExampleTheadBased2DEntity(ID, coo, 0);
		insertEntity( entity );
		entity.start();
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		int ID = generateID();
		Coordinate coo = new Coordinate(e.getX(), e.getY(), 0);
		Entity2D entity = new ExampleTheadBased2DEntity(ID, coo, 0);
		insertEntity( entity );
		entity.start();
	}

	
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_SPACE ){
			createEntity();
		}
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}
}
